//
//  CWCycleView.m
//  CycleScrollVIew
//
//  Created by lanou3g on 16/4/19.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "CWCycleView.h"
#import "UIImageView+WebCache.h"
@interface CWCycleView ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView   *imgScrollView;
@property (nonatomic, strong) UIPageControl  *imgPageControl;
@property (nonatomic, strong) NSMutableArray *imgViewArray;
@property (nonatomic, strong) NSTimer        *imgTimer;
@property (nonatomic, strong) NSMutableArray        *titleLabels;
//当前展示的图片下标
@property (nonatomic, assign) NSInteger index;

//点击回调block
@property (nonatomic, copy) void(^tapBlock)(NSInteger index);

//时间间隔
@property (nonatomic, assign) NSTimeInterval interval;
@end

@implementation CWCycleView
//初始化
- (instancetype)initWithFrame:(CGRect)frame Interval:(NSTimeInterval)interval {
    if (self = [super initWithFrame:frame]) {
        self.imgViewArray = [NSMutableArray new];
        self.titleLabels = [NSMutableArray new];
        self.interval = interval;
        self.imgScrollView = [[UIScrollView alloc]initWithFrame:self.bounds];
        self.imgScrollView.contentSize = CGSizeMake(frame.size.width * 3, frame.size.height);
        self.imgScrollView.pagingEnabled = YES;
        self.imgScrollView.showsHorizontalScrollIndicator = NO;
        self.imgScrollView.delegate = self;
        self.imgPageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, frame.size.height - 80, frame.size.width, 40)];
        
        
        [self addSubview:self.imgScrollView];
        for (int i = 0; i < 3; i++) {
            UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(frame.size.width * i, 0, frame.size.width, frame.size.height - 45)];
            UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(frame.size.width * i + 8, imgView.height + 5, frame.size.width - 16, 40)];
            titleLabel.textColor = [UIColor blackColor];
            titleLabel.font = [UIFont systemFontOfSize:16];
            titleLabel.numberOfLines = 0;
            //添加到夫视图,数组
            [self.imgScrollView addSubview:imgView];
            [self.imgScrollView addSubview:titleLabel];
            [self.imgViewArray addObject:imgView];
            [self.titleLabels addObject:titleLabel];
        }
        [self addSubview:self.imgPageControl];
    }
    return self;
}


#pragma mark 切换至夜间模式
- (void)updateToNightSkinMode {
    for (int i = 0; i < 3; i++) {
        UILabel *label = self.titleLabels[i];
        label.textColor = [UIColor grayColor];
    }
    self.backgroundColor = [UIColor colorWithRed:42/255.0 green:39/255.0 blue:43/255.0 alpha:1.0];
}
- (void)updateToDaySkinMode {
    for (int i = 0; i < 3; i++) {
        UILabel *label = self.titleLabels[i];
        label.textColor = [UIColor blackColor];
    }
    self.backgroundColor = [UIColor whiteColor];
}

//图片URL数组
- (void)setImgUrlArray:(NSMutableArray *)imgUrlArray {
    if (_imgUrlArray != imgUrlArray) {
        _imgUrlArray = imgUrlArray;
       
        [self.imgScrollView setContentOffset:CGPointMake(self.frame.size.width, 0)];
        [self layoutImagesWithUrl];
        [self addTimer];
        [self setupPageControl];
        [self addTapGesture];
    }
}

//标题数组
//- (void)setTitles:(NSMutableArray *)titles {
//    if (_titles != titles) {
//        _titles = titles;
//    }
//}

//添加点击手势
- (void)addTapGesture {
    //点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCycleViewAction:)];
    [self.imgViewArray[1] addGestureRecognizer:tap];
    [(UIImageView *)self.imgViewArray[1] setUserInteractionEnabled:YES];
}

//设置PageControl
- (void)setupPageControl {
    self.imgPageControl.enabled = NO;
    self.imgPageControl.currentPage = 0;
    self.imgPageControl.numberOfPages = _imgUrlArray.count;
}



//添加定时器
- (void)addTimer {
    [self removeTimer];
    //初始化timer
    if (self.interval <= 0) {
        self.imgTimer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }else {
        self.imgTimer = [NSTimer scheduledTimerWithTimeInterval:self.interval target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
    }
}

- (void)removeTimer {
    [self.imgTimer invalidate];
    self.imgTimer = nil;
}

#pragma mark - UIScrollViewDelegate
static CGFloat x = 0;
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    x = scrollView.contentOffset.x;
    [self.imgTimer setFireDate:[NSDate distantFuture]];
}
//减速动画结束
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (x > scrollView.contentOffset.x) {
        [self panToLeft:NO Index:self.index];
    }else if (x < scrollView.contentOffset.x) {
        [self panToLeft:YES Index:self.index];
    }
    [self.imgTimer setFireDate:[NSDate dateWithTimeIntervalSinceNow:2]];
}

#pragma mark - 想做滑动,向右滑动结束切换
- (void)panToLeft:(BOOL)left Index:(NSInteger)index {
    if (!left) {
        self.index = (index - 1 + _imgUrlArray.count) % _imgUrlArray.count;
    }else {
        self.index = (index + 1) % _imgUrlArray.count;
    }
    [self layoutImagesWithUrl];
    //回位操作
    [self.imgScrollView setContentOffset:CGPointMake(self.frame.size.width, 0) animated:NO];
}



- (void)layoutImagesWithUrl {
    for (int i = 0; i < 3; i++) {
        [self.imgViewArray[i] sd_setImageWithURL:self.imgUrlArray[(self.index - 1 + i + _imgUrlArray.count) % _imgUrlArray.count]placeholderImage:[UIImage imageNamed:@"placeholder"]];
        [(UILabel *)self.titleLabels[i] setText:self.titles[(self.index - 1 + i + _imgUrlArray.count) % _imgUrlArray.count]];
    }
}
//index的setter方法
- (void)setIndex:(NSInteger)index {
    if (_index != index) {
        _index = index;
        self.imgPageControl.currentPage = index;
    }
}

- (void)timerAction {
    [self.imgScrollView setContentOffset:CGPointMake(self.imgScrollView.frame.size.width * 2, 0)animated:YES];
    self.index = (self.index + 1) % _imgUrlArray.count;
   
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self.imgScrollView setContentOffset:CGPointMake(scrollView.frame.size.width, 0)animated:NO];
    [self layoutImagesWithUrl];
}

//点击手势
- (void)tapCycleViewAction:(UITapGestureRecognizer *)tap {
    if (self.tapBlock) {
        self.tapBlock(self.index);
    }
}

//添加block
- (void)addTapBlock:(void (^)(NSInteger))block {
    self.tapBlock = block;
}
@end
